package com.yulian.repository.supplier_management;

import java.util.Iterator;

import com.healthmarketscience.sqlbuilder.CustomSql;
import com.healthmarketscience.sqlbuilder.InCondition;
import com.healthmarketscience.sqlbuilder.SelectQuery;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.Log4JLoggerFactory;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.Tuple;

public class GoodsParameterRepositoryImpl {

    final InternalLogger logger = Log4JLoggerFactory.getInstance(GoodsParameterRepositoryImpl.class);

    /**
     * 获取记录总数
     *
     * @param conn
     * @param queryJSObj
     * @return
     */
    public Future<RowSet<Row>> getRecordsTotal(SqlConnection conn, JsonObject queryJSObj) {
        Promise<RowSet<Row>> promise = Promise.promise();
        //首先取出符合条件的总的记录数
        String sql = this.creaStrGetRecordsTotalWithJson(queryJSObj);
        conn
        .preparedQuery(sql)
        .execute(ar2 -> {
            //Release the connection to the pool
            conn.close();
            if (ar2.succeeded()) {
                promise.complete(ar2.result());
            } else {
                logger.info("getRecordsTotal, 失败原因: {}", ar2.cause().getMessage());
                //这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar2.cause());
            }
        });
        return promise.future();
    }

    /**
     * 创建查询符合搜索条件的记录数的 sql语句
     *
     * @param queryJS
     * @return
     */
    public String creaStrGetRecordsTotalWithJson(JsonObject queryJS) {

        // define SelectQuery object
        SelectQuery sq = new SelectQuery();
        sq.addCustomColumns(new CustomSql(" count(*) as total "))
        .addCustomFromTable("goods_parameter");

        //common part to add InCondition
        Iterator<String> iter = queryJS.fieldNames().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            try {
                String value = queryJS.getString(key);
                if (!value.equals("") && !value.equals("[]")) {
                    String firstChar = value.substring(0, 1);
                    if (firstChar.equals("["))
                        value = value.substring(1, value.length() - 1);
                    value = value.replace(", ", "','");
                    //添加where  InCondtion 子句
                    sq.getWhereClause().addCustomCondition(new InCondition(new CustomSql(key), value));
                }

            } catch (Exception e) {
                // Something went wrong!
            }
        }
        return sq.validate().toString();

    }

    /**
     * 获取记录
     *
     * @param conn
     * @param queryJSObj
     * @param query_offset
     * @param query_limit
     * @return
     */
    public Future<RowSet<Row>> getRecords(SqlConnection conn, JsonObject queryJSObj, int query_offset, int query_limit) {
        Promise<RowSet<Row>> promise = Promise.promise();
        /***
         * get_records with queryJSObj
         */
        //取出满足条件的记录
        String sql = this.creaStrGetRecordsWithJson(queryJSObj, query_offset, query_limit);
        conn
        .preparedQuery(sql)
        .execute(ar2 -> {
            //Release the connection to the pool
            conn.close();
            if (ar2.succeeded()) {
//		        	logger.info("getRecords, query success: "+ ar2.result().rowCount());
                promise.complete(ar2.result());
            } else {
                logger.info("getRecords, 失败原因: {}", ar2.cause().getMessage());
                //这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar2.cause());
            }
        });
        return promise.future();
    }

    /**
     * 创建查询符合搜索条件记录 的sql语句
     *
     * @param queryJS
     * @param query_offset
     * @param query_limit
     * @return
     */
    public String creaStrGetRecordsWithJson(JsonObject queryJS, int query_offset, int query_limit) {

        // define SelectQuery object
        SelectQuery sq = new SelectQuery();
        sq.addCustomColumns(new CustomSql("id"), new CustomSql("goods_gather_area_code"), new CustomSql("class_lv1_code"),
        new CustomSql("class_lv2_code"), new CustomSql("goods_sales_grade_code"), new CustomSql("variety_code"),
        new CustomSql("aggregation_code"), new CustomSql("sales_package_spec_type_code"), new CustomSql("standard_safety_stock_days"), new CustomSql("temp_safety_stock_days"), new CustomSql("safety_stock_warn_days"),
        new CustomSql("spot_purchase_replenish_price_discount"), new CustomSql("spot_purchase_stock_price_discount"),
        new CustomSql("admit_time_line"), new CustomSql("list_goods_code"), new CustomSql("list_goods_name"), new CustomSql("list_goods_grade_code"),
        new CustomSql("list_goods_status"), new CustomSql("sales_quantity_scheme_code"),
        new CustomSql("list_goods_price_mode_code"), new CustomSql("list_goods_package_weight"), new CustomSql("list_goods_package_length"),
        new CustomSql("list_goods_package_width"), new CustomSql("list_goods_package_height"), new CustomSql("list_goods_package_volume"),
        new CustomSql("list_goods_package_size"), new CustomSql("list_goods_specification_code"), new CustomSql("list_package_spec"), new CustomSql("list_goods_weight_package_spec"),
        new CustomSql("sales_package_spec"), new CustomSql("supply_chain_goods_code"), new CustomSql("supply_chain_goods_name"), new CustomSql("supply_chain_goods_grade_code"), new CustomSql("supply_chain_goods_status"),
        new CustomSql("supply_chain_goods_settle_package_spec"), new CustomSql("supply_chain_goods_algorithm_package_spec"), new CustomSql("supply_chain_goods_weight_package_spec"),
        new CustomSql("supply_chain_goods_package_weight"), new CustomSql("supply_chain_goods_price_mode_code"), new CustomSql("supply_chain_goods_package_length"),
        new CustomSql("supply_chain_goods_package_width"), new CustomSql("supply_chain_goods_package_height"), new CustomSql("supply_chain_goods_package_volume"), new CustomSql("supply_chain_goods_package_size"),
        new CustomSql("supply_chain_goods_specification_code"), new CustomSql("is_delete"), new CustomSql("create_user"), new CustomSql("update_user"), new CustomSql("create_time"), new CustomSql("update_time")
        )
        .addCustomFromTable("goods_parameter");

        //common part to add InCondition
        Iterator<String> iter = queryJS.fieldNames().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            try {
                String value = queryJS.getString(key);
                System.out.print(value);
                if (!value.equals("") && !value.equals("[]")) {
                    String firstChar = value.substring(0, 1);
                    if (firstChar.equals("["))
                        value = value.substring(1, value.length() - 1);
                    value = value.replace(", ", "','");
                    //添加where  InCondtion 子句
                    sq.getWhereClause().addCustomCondition(new InCondition(new CustomSql(key), value));
                }
            } catch (Exception e) {
                // Something went wrong!
            }
        }
        return sq.validate().toString() + " order by id desc  limit " + query_limit + " offset " + query_offset;
    }

    /**
     * 此方法判断传入的挂牌商品名是否存在
     *
     * @param conn
     * @param list_goods_name
     * @return
     */
    public Future<RowSet<Row>> getGoodsParameterByCode(SqlConnection conn, String aggregation_code, String supply_chain_goods_code, String list_goods_code) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "select * from goods_parameter where aggregation_code = $1 and supply_chain_goods_code = $2 and list_goods_code = $3  order by id desc limit 1";
        conn
        .preparedQuery(sql)
        .execute(Tuple.of(aggregation_code, supply_chain_goods_code, list_goods_code), ar -> {
            conn.close();
            if (ar.succeeded()) {
                logger.info("getListGoodsName, 记录数: ", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("getListGoodsName, 失败,失败原因:{}", ar.cause().getMessage());
                //这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }

    /**
     * 此方法判断传入的挂牌商品名是否存在
     *
     * @param conn
     * @param list_goods_name
     * @return
     */
    public Future<RowSet<Row>> getListGoodsName(SqlConnection conn, String list_goods_name) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "select list_goods_name from goods_parameter where list_goods_name = $1  order by id desc limit 1";
        conn
        .preparedQuery(sql)
        .execute(Tuple.of(list_goods_name), ar -> {
            conn.close();
            if (ar.succeeded()) {
                logger.info("getListGoodsName, 记录数: ", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("getListGoodsName, 失败,失败原因:{}", ar.cause().getMessage());
                //这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }

    /***
     *      根据传入当前记录的id以及编辑后的聚合名、挂牌商品名字、供应商商品名，判断表中是否存在三个名字一样的其他数据，如果有则说明该数据已经存在了，反之则无
     * @param conn
     * @param id
     * @param aggregation_code
     * @param list_goods_name
     * @param supply_chain_goods_name
     * @return
     */

    public Future<RowSet<Row>> getListGoodById(SqlConnection conn, long id, String aggregation_code, String list_goods_name, String supply_chain_goods_name) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "select aggregation_code,list_goods_name,supply_chain_goods_name from goods_parameter where aggregation_code = $1 and list_goods_name = $2 and supply_chain_goods_name = $3 and id != $4  order by id desc limit 1";
        conn
        .preparedQuery(sql)
        .execute(Tuple.of(aggregation_code, list_goods_name, supply_chain_goods_name, id), ar -> {
            conn.close();
            if (ar.succeeded()) {
                logger.info("getListGoodById, 记录数: ", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("getListGoodById, 失败,失败原因:{}", ar.cause().getMessage());
                //这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }

    public Future<RowSet<Row>> getListGoodsCode(SqlConnection conn) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "select list_goods_code from goods_parameter order by id desc limit 1";
        conn
        .preparedQuery(sql)
        .execute(ar -> {
            conn.close();
            if (ar.succeeded()) {
                logger.info("getListGoodsCode, 记录数: ", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("getListGoodsCode, 失败,失败原因:{}", ar.cause().getMessage());
                //这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }

    /**
     * @param conn
     * @param goods_gather_area_code
     * @param class_lv1_code
     * @param class_lv2_code
     * @param variety_code
     * @param aggregation_code
     * @param goods_sales_grade_code
     * @param sales_package_spec_type_code
     * @param temp_safety_stock_days
     * @param safety_stock_warn_days
     * @param spot_purchase_replenish_price_discount
     * @param spot_purchase_stock_price_discount
     * @param list_goods_code
     * @param list_goods_name
     * @param list_goods_grade_code
     * @param sales_quantity_scheme_code
     * @param list_goods_price_mode_code
     * @param list_goods_package_weight
     * @param list_goods_package_length
     * @param list_goods_package_width
     * @param list_goods_package_height
     * @param list_goods_package_volume
     * @param list_goods_package_size
     * @param list_goods_specification_code
     * @param supply_chain_goods_code
     * @param supply_chain_goods_name
     * @param supply_chain_goods_grade_code
     * @param supply_chain_goods_settle_package_spec
     * @param supply_chain_goods_algorithm_package_spec
     * @param supply_chain_goods_weight_package_spec
     * @param supply_chain_goods_package_weight
     * @param supply_chain_goods_price_mode_code
     * @param supply_chain_goods_package_length
     * @param supply_chain_goods_package_width
     * @param supply_chain_goods_package_height
     * @param supply_chain_goods_package_volume
     * @param supply_chain_goods_package_size
     * @param supply_chain_goods_specification_code
     * @param is_delete
     * @param create_user
     * @return
     */
    public Future<RowSet<Row>> insertGoodsParameter(SqlConnection conn, String goods_gather_area_code, String class_lv1_code, String class_lv2_code, String variety_code, String aggregation_code, String goods_sales_grade_code,
                                                    String sales_package_spec_type_code, double temp_safety_stock_days, double safety_stock_warn_days, double spot_purchase_replenish_price_discount,
                                                    double spot_purchase_stock_price_discount, String list_goods_code, String list_goods_name, String list_goods_grade_code, String sales_quantity_scheme_code, String list_goods_price_mode_code, double list_goods_package_weight,
                                                    double list_goods_package_length, double list_goods_package_width, double list_goods_package_height, double list_goods_package_volume, String list_goods_package_size,
                                                    String list_goods_specification_code, String supply_chain_goods_code, String supply_chain_goods_name, String supply_chain_goods_grade_code,
                                                    double supply_chain_goods_settle_package_spec, double supply_chain_goods_algorithm_package_spec, double supply_chain_goods_weight_package_spec, double supply_chain_goods_package_weight, String supply_chain_goods_price_mode_code, double supply_chain_goods_package_length,
                                                    double supply_chain_goods_package_width, double supply_chain_goods_package_height, double supply_chain_goods_package_volume, String supply_chain_goods_package_size, String supply_chain_goods_specification_code, String create_user) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "INSERT INTO goods_parameter (goods_gather_area_code, class_lv1_code, class_lv2_code,variety_code, aggregation_code, goods_sales_grade_code, sales_package_spec_type_code,  temp_safety_stock_days,\n"
        + " safety_stock_warn_days, spot_purchase_replenish_price_discount, spot_purchase_stock_price_discount, list_goods_code, list_goods_name, list_goods_grade_code,  sales_quantity_scheme_code, list_goods_price_mode_code,\n"
        + " list_goods_package_weight, list_goods_package_length, list_goods_package_width, list_goods_package_height, list_goods_package_volume, list_goods_package_size, list_goods_specification_code, \n"
        + " supply_chain_goods_code, supply_chain_goods_name, supply_chain_goods_grade_code, supply_chain_goods_settle_package_spec, supply_chain_goods_algorithm_package_spec, supply_chain_goods_weight_package_spec, supply_chain_goods_package_weight, supply_chain_goods_price_mode_code,\n"
        + " supply_chain_goods_package_length, supply_chain_goods_package_width, supply_chain_goods_package_height, supply_chain_goods_package_volume, supply_chain_goods_package_size, supply_chain_goods_specification_code, create_user)"
        + " VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38)  RETURNING id";
        conn
        .preparedQuery(sql)
        .execute(Tuple.of(goods_gather_area_code, class_lv1_code, class_lv2_code, variety_code, aggregation_code, goods_sales_grade_code, sales_package_spec_type_code, temp_safety_stock_days,
        safety_stock_warn_days, spot_purchase_replenish_price_discount, spot_purchase_stock_price_discount, list_goods_code, list_goods_name, list_goods_grade_code, sales_quantity_scheme_code, list_goods_price_mode_code,
        list_goods_package_weight, list_goods_package_length, list_goods_package_width, list_goods_package_height, list_goods_package_volume, list_goods_package_size, list_goods_specification_code,
        supply_chain_goods_code, supply_chain_goods_name, supply_chain_goods_grade_code, supply_chain_goods_settle_package_spec, supply_chain_goods_algorithm_package_spec, supply_chain_goods_weight_package_spec, supply_chain_goods_package_weight, supply_chain_goods_price_mode_code,
        supply_chain_goods_package_length, supply_chain_goods_package_width, supply_chain_goods_package_height, supply_chain_goods_package_volume, supply_chain_goods_package_size, supply_chain_goods_specification_code, create_user), ar2 -> {
            //释放一个连接到连接池
            conn.close();
            if (ar2.succeeded()) {
                logger.info("insertGoodsParameter, 插入记录数:{} ", ar2.result().rowCount());
                promise.complete(ar2.result());
            } else {
                logger.info("insertGoodsParameter, 插入失败,失败原因:{}", ar2.cause().getMessage());
                //用Promise来向上抛异常
                promise.fail(ar2.cause());
            }
        });
        return promise.future();
    }

    public Future<RowSet<Row>> updateGoodsParameterByCodes(SqlConnection conn, String goods_gather_area_code, String class_lv1_code, String class_lv2_code, String variety_code, String aggregation_code, String goods_sales_grade_code,
                                                           String sales_package_spec_type_code, double temp_safety_stock_days, double safety_stock_warn_days, double spot_purchase_replenish_price_discount,
                                                           double spot_purchase_stock_price_discount, String list_goods_code, String list_goods_name, String list_goods_grade_code, String sales_quantity_scheme_code, String list_goods_price_mode_code, double list_goods_package_weight,
                                                           double list_goods_package_length, double list_goods_package_width, double list_goods_package_height, double list_goods_package_volume, String list_goods_package_size,
                                                           String list_goods_specification_code, String supply_chain_goods_code, String supply_chain_goods_name, String supply_chain_goods_grade_code,
                                                           double supply_chain_goods_settle_package_spec, double supply_chain_goods_algorithm_package_spec, double supply_chain_goods_weight_package_spec, double supply_chain_goods_package_weight, String supply_chain_goods_price_mode_code, double supply_chain_goods_package_length,
                                                           double supply_chain_goods_package_width, double supply_chain_goods_package_height, double supply_chain_goods_package_volume, String supply_chain_goods_package_size, String supply_chain_goods_specification_code, String update_user) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "UPDATE goods_parameter SET goods_gather_area_code = $1, class_lv1_code = $2, class_lv2_code = $3, goods_sales_grade_code = $4, variety_code = $5, aggregation_code = $6, sales_package_spec_type_code = $7, standard_safety_stock_days = $8, temp_safety_stock_days = $9, " +
        "safety_stock_warn_days = $10, spot_purchase_replenish_price_discount = $11, spot_purchase_stock_price_discount = $12, admit_time_line = $13, list_goods_code = $14, list_goods_name = $15, list_goods_grade_code = $16, list_goods_status = $17, sales_quantity_scheme_code = $18, list_goods_price_mode_code = $19, " +
        "list_goods_package_weight = $20, list_goods_package_length = $21, list_goods_package_width = $22, list_goods_package_height = $23, list_goods_package_volume = $24, list_goods_package_size = $25, list_goods_specification_code = $26, list_package_spec = $27, list_goods_weight_package_spec = $28, sales_package_spec = $29, " +
        "supply_chain_goods_code = $30, supply_chain_goods_name = $31, supply_chain_goods_grade_code = $32, supply_chain_goods_status = $33, supply_chain_goods_settle_package_spec = $34, supply_chain_goods_algorithm_package_spec = $35, supply_chain_goods_weight_package_spec = $36, supply_chain_goods_package_weight = $37, supply_chain_goods_price_mode_code = $38, " +
        "supply_chain_goods_package_length = $39, supply_chain_goods_package_width = $40, supply_chain_goods_package_height = $41, supply_chain_goods_package_volume = $42, supply_chain_goods_package_size = $43, supply_chain_goods_specification_code = $44, is_delete = $45, create_user = $46 ,update_user = $47 WHERE id = $48 RETURNING id";
        conn.preparedQuery(sql).execute(Tuple.of(goods_gather_area_code, class_lv1_code, class_lv2_code, variety_code, aggregation_code, goods_sales_grade_code, sales_package_spec_type_code, temp_safety_stock_days,
        safety_stock_warn_days, spot_purchase_replenish_price_discount, spot_purchase_stock_price_discount, list_goods_code, list_goods_name, list_goods_grade_code, sales_quantity_scheme_code, list_goods_price_mode_code,
        list_goods_package_weight, list_goods_package_length, list_goods_package_width, list_goods_package_height, list_goods_package_volume, list_goods_package_size, list_goods_specification_code,
        supply_chain_goods_code, supply_chain_goods_name, supply_chain_goods_grade_code, supply_chain_goods_settle_package_spec, supply_chain_goods_algorithm_package_spec, supply_chain_goods_weight_package_spec, supply_chain_goods_package_weight, supply_chain_goods_price_mode_code,
        supply_chain_goods_package_length, supply_chain_goods_package_width, supply_chain_goods_package_height, supply_chain_goods_package_volume, supply_chain_goods_package_size, supply_chain_goods_specification_code, update_user), ar -> {
            // Release the connection to the pool
            conn.close();
            if (ar.succeeded()) {
                logger.info("updateGoodsParameterById, 更新记录数: {}", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("updateGoodsParameterById, 更新失败, 失败原因: {}", ar.cause().getMessage());
                // 这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }

    /***
     * 根据ID跟新GoodsParameter中的数据
     * @param conn
     * @param id
     * @param goods_gather_area_code
     * @param class_lv1_code
     * @param class_lv2_code
     * @param goods_sales_grade_code
     * @param variety_code
     * @param aggregation_code
     * @param sales_package_spec_type_code
     * @param standard_safety_stock_days
     * @param temp_safety_stock_days
     * @param safety_stock_warn_days
     * @param spot_purchase_replenish_price_discount
     * @param spot_purchase_stock_price_discount
     * @param admit_time_line
     * @param list_goods_code
     * @param list_goods_name
     * @param list_goods_grade_code
     * @param list_goods_status
     * @param sales_quantity_scheme_code
     * @param list_goods_price_mode_code
     * @param list_goods_package_weight
     * @param list_goods_package_length
     * @param list_goods_package_width
     * @param list_goods_package_height
     * @param list_goods_package_volume
     * @param list_goods_package_size
     * @param list_goods_specification_code
     * @param list_package_spec
     * @param list_goods_weight_package_spec
     * @param sales_package_spec
     * @param supply_chain_goods_code
     * @param supply_chain_goods_name
     * @param supply_chain_goods_grade_code
     * @param supply_chain_goods_status
     * @param supply_chain_goods_settle_package_spec
     * @param supply_chain_goods_algorithm_package_spec
     * @param supply_chain_goods_weight_package_spec
     * @param supply_chain_goods_package_weight
     * @param supply_chain_goods_price_mode_code
     * @param supply_chain_goods_package_length
     * @param supply_chain_goods_package_width
     * @param supply_chain_goods_package_height
     * @param supply_chain_goods_package_volume
     * @param supply_chain_goods_package_size
     * @param supply_chain_goods_specification_code
     * @param is_delete
     * @param update_user
     * @return
     */
    public Future<RowSet<Row>> updateGoodsParameterById(SqlConnection conn, long id, String goods_gather_area_code, String class_lv1_code, String class_lv2_code, String variety_code, String aggregation_code, String goods_sales_grade_code,
                                                        String sales_package_spec_type_code, int temp_safety_stock_days, float safety_stock_warn_days, double spot_purchase_replenish_price_discount,
                                                        double spot_purchase_stock_price_discount, String list_goods_code, String list_goods_name, String list_goods_grade_code, String sales_quantity_scheme_code, String list_goods_price_mode_code, double list_goods_package_weight,
                                                        double list_goods_package_length, double list_goods_package_width, double list_goods_package_height, double list_goods_package_volume, String list_goods_package_size,
                                                        String list_goods_specification_code, String supply_chain_goods_code, String supply_chain_goods_name, String supply_chain_goods_grade_code,
                                                        double supply_chain_goods_settle_package_spec, double supply_chain_goods_algorithm_package_spec, double supply_chain_goods_weight_package_spec, double supply_chain_goods_package_weight, String supply_chain_goods_price_mode_code, double supply_chain_goods_package_length,
                                                        double supply_chain_goods_package_width, double supply_chain_goods_package_height, double supply_chain_goods_package_volume, String supply_chain_goods_package_size, String supply_chain_goods_specification_code, String update_user) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "UPDATE goods_parameter SET goods_gather_area_code = $1, class_lv1_code = $2, class_lv2_code = $3, goods_sales_grade_code = $4, variety_code = $5, aggregation_code = $6, sales_package_spec_type_code = $7, standard_safety_stock_days = $8, temp_safety_stock_days = $9, " +
        "safety_stock_warn_days = $10, spot_purchase_replenish_price_discount = $11, spot_purchase_stock_price_discount = $12, admit_time_line = $13, list_goods_code = $14, list_goods_name = $15, list_goods_grade_code = $16, list_goods_status = $17, sales_quantity_scheme_code = $18, list_goods_price_mode_code = $19, " +
        "list_goods_package_weight = $20, list_goods_package_length = $21, list_goods_package_width = $22, list_goods_package_height = $23, list_goods_package_volume = $24, list_goods_package_size = $25, list_goods_specification_code = $26, list_package_spec = $27, list_goods_weight_package_spec = $28, sales_package_spec = $29, " +
        "supply_chain_goods_code = $30, supply_chain_goods_name = $31, supply_chain_goods_grade_code = $32, supply_chain_goods_status = $33, supply_chain_goods_settle_package_spec = $34, supply_chain_goods_algorithm_package_spec = $35, supply_chain_goods_weight_package_spec = $36, supply_chain_goods_package_weight = $37, supply_chain_goods_price_mode_code = $38, " +
        "supply_chain_goods_package_length = $39, supply_chain_goods_package_width = $40, supply_chain_goods_package_height = $41, supply_chain_goods_package_volume = $42, supply_chain_goods_package_size = $43, supply_chain_goods_specification_code = $44, is_delete = $45, create_user = $46 ,update_user = $47 WHERE id = $48 RETURNING id";
        conn.preparedQuery(sql).execute(Tuple.of(goods_gather_area_code, class_lv1_code, class_lv2_code, variety_code, aggregation_code, goods_sales_grade_code, sales_package_spec_type_code, temp_safety_stock_days,
        safety_stock_warn_days, spot_purchase_replenish_price_discount, spot_purchase_stock_price_discount, list_goods_code, list_goods_name, list_goods_grade_code, sales_quantity_scheme_code, list_goods_price_mode_code,
        list_goods_package_weight, list_goods_package_length, list_goods_package_width, list_goods_package_height, list_goods_package_volume, list_goods_package_size, list_goods_specification_code,
        supply_chain_goods_code, supply_chain_goods_name, supply_chain_goods_grade_code, supply_chain_goods_settle_package_spec, supply_chain_goods_algorithm_package_spec, supply_chain_goods_weight_package_spec, supply_chain_goods_package_weight, supply_chain_goods_price_mode_code,
        supply_chain_goods_package_length, supply_chain_goods_package_width, supply_chain_goods_package_height, supply_chain_goods_package_volume, supply_chain_goods_package_size, supply_chain_goods_specification_code, update_user, id), ar -> {
            // Release the connection to the pool
            conn.close();
            if (ar.succeeded()) {
                logger.info("updateGoodsParameterById, 更新记录数: {}", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("updateGoodsParameterById, 更新失败, 失败原因: {}", ar.cause().getMessage());
                // 这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }

    /**
     * @param conn
     * @param is_delete
     * @param id_list
     * @param update_user
     * @return
     */
    public Future<RowSet<Row>> deleteGoodsParameter(SqlConnection conn, int is_delete, String id_list, String update_user) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "update goods_parameter set is_delete = $1, update_user = $2 where id = any(array" + id_list + ") RETURNING id";
        conn
        .preparedQuery(sql)
        .execute(Tuple.of(is_delete, update_user), ar -> {
            conn.close();
            if (ar.succeeded()) {
                logger.info("deleteGoodsParameter, 逻辑删除记录数:{} ", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("deleteGoodsParameter, 逻辑删除失败,失败原因:{}", ar.cause().getMessage());
                //用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }

    /**
     * @param conn
     * @param is_delete
     * @param id
     * @param update_user
     * @return
     */
    public Future<RowSet<Row>> deleteGoodsParameterById(SqlConnection conn, int is_delete, long id, String update_user) {
        Promise<RowSet<Row>> promise = Promise.promise();
        String sql = "update goods_parameter set is_delete = $1, update_user = $2 where id = $3 RETURNING id";
        conn
        .preparedQuery(sql)
        .execute(Tuple.of(is_delete, update_user, id), ar -> {
            conn.close();
            if (ar.succeeded()) {
                logger.info("deleteGoodsParameterById, 逻辑删除记录数:{} ", ar.result().rowCount());
                promise.complete(ar.result());
            } else {
                logger.info("deleteGoodsParameterById, 逻辑删除失败,失败原因:{}", ar.cause().getMessage());
                //用Promise来向上抛异常
                promise.fail(ar.cause());
            }
        });
        return promise.future();
    }


}
 
